package cn.tedu.mall.book.controller;

import cn.tedu.mall.book.pojo.dto.CartAddNewDTO;
import cn.tedu.mall.book.pojo.vo.CartProductVO;
import cn.tedu.mall.book.pojo.vo.ProductClearVO;
import cn.tedu.mall.book.service.ICartService;
import cn.tedu.mall.book.web.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zxjun
 * @date 2022/6/25 10:54
 */
@RestController
@Api(tags = "购物车管理")
@RequestMapping(value = "/carts",produces = "application/json;charset=utf-8")
public class CartController {
    @Autowired
    private ICartService cartService;
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(@RequestBody CartAddNewDTO cartAddNewDTO){
        System.out.println("cartAddNewDTO = " + cartAddNewDTO);
        cartService.addNew(cartAddNewDTO);
        return JsonResult.ok();
    }

    @GetMapping("/change/{id}/{value}")
    public JsonResult<Void> changeCountById(@PathVariable("id") Integer id,@PathVariable("value") Integer value){
        cartService.changeCountById(id,value);
        return JsonResult.ok();
    }

    @GetMapping("/{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable Integer id){
        cartService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/myCart")
    public JsonResult<List<CartProductVO>> getByUserId(){
        return JsonResult.ok(cartService.getByUserId());
    }

    @GetMapping("/clear")
    public JsonResult<List<ProductClearVO>> getByCartIds(Integer[] ids){
        return JsonResult.ok(cartService.getByCartId(ids));
    }
}
